package com.ht.web.student;

import com.ht.bean.education.Classtype;
import com.ht.bean.education.Stureply;
import com.ht.bean.education.Stuscore;
import com.ht.bean.emp.Empinfo;
import com.ht.bean.student.*;
import com.ht.bean.json.JsonData;
import com.ht.service.education.ClasstypeService;
import com.ht.service.education.StureplyService;
import com.ht.service.education.StuscoreService;
import com.ht.service.emp.EmpinfoService;
import com.ht.service.student.*;
import com.ht.util.Pager;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lrq
 * @date 2020/6/9 8:51
 * 学生管理
 */
@Controller
@RequestMapping("stu")
public class StuController {
    @Resource
    private StudentService studentService;//学生
    @Resource
    private StudentfloorService studentfloorService;//楼栋
    @Resource
    private StudenthuorService studenthuorService;//宿舍
    @Resource
    private StudentclassService studentclassService;//班级
    @Resource
    private MajorService majorService;//专业
    @Resource
    private StudentfallService studentfallService;//届别
    @Resource
    private educationMapperService educationMapperService;//教育经历
    @Resource
    private FamilycaseService familycaseService;//家庭情况
    @Resource
    private AtschoolService atschoolService;//在校情况
    @Resource
    private StuscoreService stuscoreService;//学生成绩
    @Resource
    private StureplyService stureplyService;//学生答辩成绩
    @Resource
    private ClasstypeService classtypeService;//班级类别接口
    @Resource
    private EmpinfoService empinfoService;//班级类别接口
    //去学生页面
    @RequestMapping("stulistUi")
    public String stulistUi(Map map) {
        List<Studentclass> studentclasses= studentclassService.selList();//查找学生班级
        map.put("studentclasses", studentclasses);
        return"student/stulist";
    }
    //去查看
    @RequestMapping("select")
    public String select(Integer studid,Map map){

        Student student=studentService.selectByPrimaryKey(studid);
        if(biye(student.getStudid())){
            Studenthuor studenthuorList= studenthuorService.selectByPrimaryKey(student.getHuor());//查找学生宿舍
            if(studenthuorList==null){
                studenthuorList=new Studenthuor();
                studenthuorList.setHuorname("空");
            }
            Studentclass studentclasses= studentclassService.selectByPrimaryKey(student.getClazz());//查找学生班级
            Empinfo empinfo=empinfoService.selectByPrimaryKey(Integer.valueOf(student.getIntroduretech()));
            List<Classtype> classtypeList=classtypeService.alldata();//班级类别
            map.put("student", student);
            map.put("studentclasses", studentclasses);
            map.put("empinfo", empinfo);
            map.put("studenthuorList", studenthuorList);
            map.put("classtypeList",classtypeList);
            return "student/stusel";
        }

        return "student/biye";
    }
    //重置密码
    @RequestMapping("reset")
    @ResponseBody
    public String reset(Integer studid){
        Student student=studentService.selectByPrimaryKey(studid);
        student.setPassword("123456");
        studentService.updateByPrimaryKey(student);
        return "true";
    }
    /**
     * @param
     * @return
     * 去添加页面
     */
    @RequestMapping("toadd")
    public String toadd(Map map){
        List<Studentfall> studentfallList=studentfallService.selList();//查找所有届别
        List<Studentfloor> studentfloorList= studentfloorService.selList();//查找所有楼栋
        List<Classtype> classtypeList=classtypeService.alldata();//班级类别
        map.put("studentfallList", studentfallList);
        map.put("studentfloorList", studentfloorList);
        map.put("classtypeList",classtypeList);
        return "student/stuadd";
    }

    @RequestMapping("ll")
    public String ll(){
        return "managerui/province";
    }
    @ResponseBody
    @RequestMapping("byhuorid")
    public List byhuorid(int floorId){
        List<Studenthuor> l=studenthuorService.selByFid(floorId);
        return l;
    }

    @ResponseBody
    @RequestMapping("bymajorid")
    public List bymajorid(int deptid){
        List<Major> l=majorService.byDeptId(deptid);
        return l;
    }

    @ResponseBody
    @RequestMapping("byfallid")
    public List byfallid(Integer classno){
        List<Studentclass> l=studentclassService.byFallId(classno);
        return l;
    }
    @Resource
    private JsonData jsonData;
    //layui加载数据
    @RequestMapping("studata")
    @ResponseBody
    public JsonData empdata(@Param("limit")int limit , @Param("page")int page,@Param("stu")Student stu ){
        Pager pager=new Pager();
        pager.setCurrPage(page);
        pager.setPageSize(limit);
        jsonData.setCount(studentService.getTotalRow(stu));
        jsonData.setCode(0);
        jsonData.setData(studentService.allPageStu(pager,stu));
        return jsonData;
    }



    /**
     * @param student
     * @return
     * 添加学生信息
     */
    @RequestMapping("add")
    @ResponseBody
    public String add(Student student, HttpSession session){
        Empinfo empinfo=(Empinfo)session.getAttribute("user");
        student.setIntroduretech(String.valueOf(empinfo.getEmp_id()));
        student.setPassword("123456");
        student.setHuor(0);
        student.setStat("在读");
        studentService.insert(student);

        Studentclass studentclass=new Studentclass();
        int conut=studentService.selbyclassid(student.getClazz());
        studentclass.setCount(conut);
        studentclass.setClassid(student.getClazz());
         studentclassService.updateByPrimaryKeySelective(studentclass);

        Studenthuor studenthuor=new Studenthuor();
        int conut2=studentService.selbyhuorid(student.getHuor());
        studenthuor.setCount(conut2);
        studenthuor.setHourid(student.getHuor());
         studenthuorService.updateByPrimaryKeySelective(studenthuor);
        return "true";
    }


    public Boolean biye(Integer studid){
        Boolean bool=null;
        Student stu=studentService.selectByPrimaryKey(studid);
        if("毕业".equals(stu.getStat())){
            bool=false;
        }else {
            bool=true;
        }
        return bool;
    }
    /**
     * @param student
     * @return
     * 修改学生信息
     */
    @RequestMapping("upd")
    @ResponseBody
    public String upd(Student student,String clazzend,String huorend){
        String ss="";
        if(biye(student.getStudid())){
            int updatacurr= studentService.updateByPrimaryKeySelective(student);
            ss="true";
        }else {
            ss="false";
        }
        return ss;
    }

    @RequestMapping("clazzupd")
    @ResponseBody
    public String clazzupd(Student student,String clazzend,String huorend){
        String ss="";
        if(biye(student.getStudid())){
            int updatacurr= studentService.updateByPrimaryKeySelective(student);
            ss="true";
        }else {
            ss="false";
        }
        //新班级
        Studentclass studentclass=new Studentclass();
        int conut=studentService.selbyclassid(student.getClazz());
        studentclass.setCount(conut);
        studentclass.setClassid(student.getClazz());
        studentclassService.updateByPrimaryKeySelective(studentclass);
        //旧班级
        Studentclass studentclassend=new Studentclass();
        int conutclazz=studentService.selbyclassid(Integer.valueOf(clazzend));
        studentclassend.setCount(conutclazz);
        studentclassend.setClassid(Integer.valueOf(clazzend));
        studentclassService.updateByPrimaryKeySelective(studentclassend);
        return ss;
    }
    @RequestMapping("huorupd")
    @ResponseBody
    public String huorupd(Student student,String clazzend,String huorend){
        String ss="";
        if (student.getHuor()==null) {
            ss="ko";
        }else {
            Studenthuor studenthuor1= studenthuorService.selectByPrimaryKey(student.getHuor());
            if(studenthuor1.getCount()>=studenthuor1.getNumberbeds()){
                ss="ko";
            }else {
                if(biye(student.getStudid())){
                    int updatacurr= studentService.updateByPrimaryKeySelective(student);
                    ss="true";
                }else {
                    ss="false";
                }
            }
        }
        //新宿舍
        Studenthuor studenthuor=new Studenthuor();
        int conut2=studentService.selbyhuorid(student.getHuor());
        studenthuor.setCount(conut2);
        studenthuor.setHourid(student.getHuor());
        studenthuorService.updateByPrimaryKeySelective(studenthuor);

        //旧宿舍
        Studenthuor studenthuorend=new Studenthuor();
        int conut1=studentService.selbyhuorid(Integer.valueOf(huorend));
        studenthuorend.setCount(conut1);
        studenthuorend.setHourid(Integer.valueOf(huorend));
        studenthuorService.updateByPrimaryKeySelective(studenthuorend);
        return ss;
    }
    //搬离宿舍
    @RequestMapping("updmv")
    @ResponseBody
    public String updmv(Integer studid){
        Student stu=studentService.selectByPrimaryKey(studid);
        int s=stu.getHuor();
        stu.setHuor(0);
        studentService.updateByPrimaryKeySelective(stu);
        ;
        Studenthuor studenthuor=new Studenthuor();
        int conut2=studentService.selbyhuorid(s);
        System.out.println(conut2);
        studenthuor.setCount(conut2);
        studenthuor.setHourid(s);
        studenthuorService.updateByPrimaryKeySelective(studenthuor);
        return "true";
    }
    //设置毕业
    @RequestMapping("setgraduate")
    @ResponseBody
    public String setgraduate(Integer studid){
        Student stu=studentService.selectByPrimaryKey(studid);
        int s=stu.getClazz();
        int h=stu.getHuor();
        stu.setStat("毕业");
        stu.setTuixue("毕业");
        stu.setXiuxue("毕业");
        stu.setGrants("0");
        stu.setHuor(0);
        stu.setClazz(0);

        studentService.updateByPrimaryKeySelective(stu);

        Studentclass studentclass=new Studentclass();
        int conut=studentService.selbyclassid(s);
        studentclass.setCount(conut);
        studentclass.setClassid(s);
        studentclassService.updateByPrimaryKeySelective(studentclass);
        ;
        Studenthuor studenthuor=new Studenthuor();
        int conut2=studentService.selbyhuorid(h);
        studenthuor.setCount(conut2);
        studenthuor.setHourid(h);
        studenthuorService.updateByPrimaryKeySelective(studenthuor);

        return "true";
    }
    /**
     * @param studid
     * @return
     * 删除学生
     */
    @RequestMapping("del")
    public String del(int studid){
        studentService.deleteByPrimaryKey(studid);

        Studentclass studentclass=new Studentclass();
        Student stu=studentService.selectByPrimaryKey(studid);
        int conut=studentService.selbyclassid(stu.getClazz());
        studentclass.setCount(conut);
        studentclass.setClassid(stu.getClazz());
        studentclassService.updateByPrimaryKeySelective(studentclass);

        Studenthuor studenthuor=new Studenthuor();
        int conut2=studentService.selbyhuorid(stu.getHuor());
        studenthuor.setCount(conut2);
        studenthuor.setHourid(stu.getHuor());
        studenthuorService.updateByPrimaryKeySelective(studenthuor);
        return "redirect:stu/stulistUi";
    }

    //根据学生id查找 教育经历
    @RequestMapping("row")
    @ResponseBody
    public JsonData row(Integer studid,HttpSession session){
        session.setAttribute("toadde",studid);
        JsonData jsonData=new JsonData();
        jsonData.setCount(educationMapperService.getTotalRow(studid));
        jsonData.setCode(0);
        jsonData.setData(educationMapperService.allStuEducation(studid));
        return jsonData;
    }
    //删除教育经历
    @RequestMapping("edel")
    @ResponseBody
    public String edel(Integer eid){
        educationMapperService.deleteByPrimaryKey(eid);
        return "true";
    }

    //去修改教育经历
    @RequestMapping("toupde")
    public String eupd(Integer eid,Map map){
        educationStu educationStu=educationMapperService.byEducationStuid(eid);
        map.put("educationStu",educationStu);
        return "student/educationStuUpd";
    }

    //修改教育经历
    @RequestMapping("upde")
    @ResponseBody
    public String upde(educationStu educationStu){
        educationMapperService.updateByPrimaryKey(educationStu);
        return "true";
    }

    //去添加教育经历
    @RequestMapping("toadde")
    public String toadde(String studid,Map map,HttpSession session){
        Student student= studentService.selectByPrimaryKey((Integer) session.getAttribute("toadde"));
        map.put("student",student);
        return "student/educationStuAdd";
    }
    //添加教育经历
    @RequestMapping("adde")
    @ResponseBody
    public String adde(educationStu educationStu){
        educationMapperService.insert(educationStu);
        return "true";
    }
    //根据学生id查找 家庭情况
    @RequestMapping("row1")
    @ResponseBody
    public JsonData row1(Integer studid,HttpSession session){
        session.setAttribute("toadde",studid);
        JsonData jsonData=new JsonData();
        jsonData.setCount(familycaseService.getTotalRow(studid));
        jsonData.setCode(0);
        jsonData.setData(familycaseService.allFamilycase(studid));
        return jsonData;
    }
    //删除家庭情况
    @RequestMapping("fdel")
    @ResponseBody
    public String fdel(Integer id){
        familycaseService.deleteByPrimaryKey(id);
        return "true";
    }

    //去修改家庭情况
    @RequestMapping("toupdf")
    public String toupdf(Integer id,Map map){
        Familycase familycase=familycaseService.selectByPrimaryKey(id);
        map.put("familycase",familycase);
        return "student/familycaseUpd";
    }

    //修改家庭情况
    @RequestMapping("updf")
    @ResponseBody
    public String updf(Familycase familycase){
        familycaseService.updateByPrimaryKey(familycase);
        return "true";
    }

    //去添加家庭情况
    @RequestMapping("toaddf")
    public String toaddf(String studid,Map map,HttpSession session){
        Student student= studentService.selectByPrimaryKey((Integer) session.getAttribute("toadde"));
        map.put("student",student);
        return "student/familycaseAdd";
    }
    //添加家庭情况
    @RequestMapping("addf")
    @ResponseBody
    public String addf(Familycase familycase){
        familycaseService.insert(familycase);
        return "true";
    }
    //根据学生id查找 在校情况
    @RequestMapping("row2")
    @ResponseBody
    public JsonData row2(Integer studid,HttpSession session){
        session.setAttribute("toadde",studid);
        JsonData jsonData=new JsonData();
        jsonData.setCount(atschoolService.getTotalRow(studid));
        jsonData.setCode(0);
        jsonData.setData(atschoolService.allAtschool(studid));
        return jsonData;
    }
    //删除在校情况
    @RequestMapping("adel")
    @ResponseBody
    public String adel(Integer id){
        atschoolService.deleteByPrimaryKey(id);
        return "true";
    }

    //去修改在校情况
    @RequestMapping("toupda")
    public String toupda(Integer id,Map map){
        Atschool atschool=atschoolService.selectByPrimaryKey(id);
        map.put("atschool",atschool);
        return "student/atschoolUpd";
    }

    //修改在校情况
    @RequestMapping("upda")
    @ResponseBody
    public String upda(Atschool atschool){
        atschoolService.updateByPrimaryKeySelective(atschool);
        return "true";
    }

    //去添加在校情况
    @RequestMapping("toadda")
    public String toadda(String studid,Map map,HttpSession session){
        Student student= studentService.selectByPrimaryKey((Integer) session.getAttribute("toadde"));
        map.put("student",student);
        return "student/atschoolAdd";
    }
    //添加在校情况
    @RequestMapping("adda")
    @ResponseBody
    public String adda(Atschool atschool,HttpSession session){

        Empinfo emp= (Empinfo)session.getAttribute("user");
        atschool.setRecordtime(new SimpleDateFormat("yyyy-MM-dd ").format(new Date()));
        atschool.setRecordson(emp.getEmp_id());
        atschool.setEmpname(emp.getEmp_name());
        atschoolService.insert(atschool);
        return "true";
    }

    //根据学生id查找 学生成绩
    @RequestMapping("row4")
    @ResponseBody
    public JsonData row4(Integer studid,HttpSession session){
        Pager pager=new Pager();
        pager.setCurrPage(1);
        pager.setPageSize(10);
        JsonData jsonData=new JsonData();
        jsonData.setCount(stuscoreService.getTotalRow(studid));
        jsonData.setCode(0);
        jsonData.setData(stuscoreService.getStuScore(pager,studid));
        return jsonData;
    }
    @RequestMapping("Stuscoredel")
    @ResponseBody
    public String Stuscoredel(Integer stuid){
        stuscoreService.deleteByPrimaryKey(stuid);
        return "true";
    }
    @RequestMapping("toupdStuscore")
    public String toupdStuscore(Integer ssid,Map map){
        Stuscore stuscore=stuscoreService.selectByPrimaryKey(ssid);
        Student student=studentService.selectByPrimaryKey(stuscore.getStuid());
        map.put("stuscore",stuscore);
        map.put("student",student);
        return "student/stuscoreUpd";
    }
    @RequestMapping("updStuscore")
    @ResponseBody
    public String updStuscore(Stuscore stuscore){
        stuscoreService.updateByPrimaryKeySelective(stuscore);
        return "true";
    }


    //根据学生id查找   学生答辩成绩
    @RequestMapping("row3")
    @ResponseBody
    public JsonData row3(Integer studid,HttpSession session){
        Pager pager=new Pager();
        pager.setCurrPage(1);
        pager.setPageSize(10);
        JsonData jsonData=new JsonData();
        jsonData.setCount(stureplyService.getTotalRow(studid));
        jsonData.setCode(0);
        jsonData.setData(stureplyService.getStuReply(pager,studid));
        return jsonData;
    }
    @RequestMapping("Stureplydel")
    @ResponseBody
    public String Stureplydel(Integer stuid){
        stureplyService.deleteByPrimaryKey(stuid);
        return "true";
    }
    @RequestMapping("toupdStureply")
    public String toupdStureply(Integer srid,Map map){
        Stureply stureply=stureplyService.selectByPrimaryKey(srid);
        Student student=studentService.selectByPrimaryKey(stureply.getStuid());
        map.put("stureply",stureply);
        map.put("student",student);
        return "student/stureplyUpd";
    }
    @RequestMapping("updStureply")
    @ResponseBody
    public String updStureply(Stureply Stureply){
        stureplyService.updateByPrimaryKeySelective(Stureply);
        return "true";
    }


    //去到调整宿舍页面
    @RequestMapping("toadjustdorm")
    public String toadjustdorm(Integer id, Model model){
        Student student=studentService.selectByPrimaryKey(id);
        List<Studentfloor> studentfloorList= studentfloorService.selList();
        model.addAttribute("stu", student);
        model.addAttribute("studentfloorList", studentfloorList);
        return "student/adjustdorm";
    }

    //去到调整班级页面
    @RequestMapping("toadjustclass")
    public String toadjustclass(Integer id,Model model){
        Student student=studentService.selectByPrimaryKey(id);
        List<Studentfall> studentfallList=studentfallService.selList();
        model.addAttribute("stu", student);
        model.addAttribute("studentfallList", studentfallList);
        return "student/adjustclass";
    }

    //去到调整学籍页面
    @RequestMapping("toadjustroll")
    public String toadjustroll(Integer id,Model model){
        List<Studentfall> studentfallList=studentfallService.selList();
        model.addAttribute("studentfallList", studentfallList);
        return "student/adjustroll";
    }


    //去到是否送电脑页面
    @RequestMapping("sendcomputer")
    public String sendcomputer(Integer id,Model model){
        Student student=studentService.selectByPrimaryKey(id);
        model.addAttribute("student", student);
        return "student/sendcomputer";
    }
    //是否领用电脑
    @RequestMapping("receivecomputer")
    public String receivecomputer(Integer id,Model model){
        Student student=studentService.selectByPrimaryKey(id);
        model.addAttribute("student", student);
        return "student/receivecomputer";
    }
    //是否享受助学金
    @RequestMapping("enjoymoney")
    public String enjoymoney(Integer id,Model model){
        Student student=studentService.selectByPrimaryKey(id);
        model.addAttribute("student", student);
        return "student/enjoymoney";
    }

}
